# -*- coding: utf-8 -*-
from flask import jsonify, request, abort
from . import app
from com.zzh.zzhpay.alipay import alipayInit


# @app.route('/ali_payment1', methods=['GET'])
# def ali_payment1():
#     params = {
#         'out_trade_no': '208591698222',  # 商户订单号
#         'subject': 'Princetest',
#         'logistics_type': 'DIRECT',
#         'logistics_fee': '0',
#         'logistics_payment': 'SELLER_PAY',
#         'price': '100',
#         'quantity': '10',
#         'return_url': 'http://localhost:5000/test.html',
#         'notify_url': 'http://localhost:5000/notify.html'
#     }
#     #
#
#     # alipay_key = 'MIIEowIBAAKCAQEA00a3nqLLL+OEkwFvjSlsWOvYS1CBAeY3XtGiZJ423SGzvu8oJFTJ9FqvRNc48rRq8t9BIXFogodTj8su/28eE7/SgPAAIaEk+Am+V83VtQqHmSd8V2iPRqNcaYKG3Cl6IrRMDzFQRhuLCRU64ndPwWPuUEeokEGphsz2+8WasOt0rxeHXqw1lFuIh27bjwErvP3de3C5TBObxKEbBiJ8hDqWUMLBlpYwPe59gVBtAkdESwurnJSpB37bBCbSi9Igx9JaSPTJ2fI98+DUxruQdd72MFv27HL0L8MtBM9N4B5K3Yb6f0BrWEAYKUwKWPg7oaWdoAH9+MlyW/iVX5NT8wIDAQABAoIBAG6atePpa5+dZzkiZkirJ9qBVVFQSsk4wUH1UyODpEstqpDJWOV0MemnU9whI+IDS33sHIeqfEDhIYDEqMz2WfOS11vCyP7SAq2LREdpLXjCk2w9PJ3x0hfzy5w8xecaG6NxaeQGN/UL1ESkBZirR2iBrS//46HpjhtQz/fMYgg9t96J/nAZzbt2Wqzxkwq7/j1plc2PFfbsbrdS0uy/o2Np2lzAzWOFdY/O1BdXe50ilm+Dc5WD6TMAfL0/NO6yg98i2LVV3rQReBRUULqTQE5TiPUexbJDihTS20Zpehd/zaxWVFLBhsnW9wLBvF7l1MaA/34TQaUZaZ+ux5ub7MkCgYEA7eZX5xovTHl44GWwp6nZLDwqKCpzJCQgAj5PUjstRqa/tgAGO44l4WzzFviz/LaFeOUyyXR0Xaw0R6eCZuBRXn193gzk2nXwH8IfVrph6lXPqKgKXNORRxgaHbfulvC5M1liWlbh93F60DXGpqLl1xqwRn3qmrQtQSQuH+tVdR8CgYEA41nRZlXAlL/ptPp8UwmdBLz9vOOJhWHV72ExqQoX/Ocl/qg8w9ep0j4xzcVjlOP1C0LLH55BJmColhRzoqPnWggzMVsqU5XhXETW85WL+IjG7FkFjgBtmda64oGeQ+ouFWsd6adfu2kiUiNvEia17Gw7i+xmSe7n4Jg6oHmJEq0CgYBRYn+n29lafCPSAHfSo9RbwalaXEtPjdw1aEOSihQPG3RyOsfnD9X9rFvs+9/3ECnRFfet210DvqmT6cY+k3bCsIOitx6Dbd1bv/kgnlpo5DpKJFKHNwl4HNKyX/4LPQbefjGbXiWnlPbCy+LNluSyFbtAIktj6UeiBVKSMNvnWQKBgComHrIwN7HcGoHY5m+mmpukRg5cyMQXrUfh5wkFvAjA+cRKgGl2dB9sNGOHsryJvoZF7c6A0mdBCEA6rm16oYuJdaQy2JgCc3Ndt9QU54w+H1L19hercKZSrvcLPMIuijiiUTteMltWVGyR8SeWUafxMODyBMy0I/z5e/UAUy9JAoGBAJjfJJpXaFbxjiIjRu9/wLUtfFw2SzPYFQmtQ7vvZksssstTXi0AOeZTBpD/sy/Mi3tjWBcVJRavMtxhiPpM+IkvT+3l5vpOPlDXTFjcfecuoynoAOqDxoV8IvW8VbQaoLyt36lcDQxRjQCVWgNqORkE+9jHjD2j2Qa0lBj0A6g1'
#     alipay_key = 'oopjw1ziy7kods711u9h293rb868uaz6'
#     alipay = Alipay(pid='2088102170266005', key=alipay_key, seller_id='2088102170266005')
#
#     url = alipay.create_direct_pay_by_user_url(out_trade_no='20859169844', subject='test1', total_fee='10.0',
#                                                return_url='http://www.baidu.com', notify_url='http://www.baidu.com')
#     # url = alipay.create_partner_trade_by_buyer_url(**params)
#     print url
#
#     # return url
#     return render_template('pay.html', pay_url=url)

#
# @app.route('/ali_payment', methods=['GET'])
# def ali_payment():
#     url = alipay.create_direct_pay_by_user('112233333333', 'test', 'test', 1220)
#     print url
#     return render_template('pay.html', pay_url=url)
#     # return url


@app.route('/alipay', methods=['POST'])
def alipayNoToken():
    if not request.json:
        abort(400)
    try:
        price = request.json['price']
        order_no = request.json['orderNo']
        subject = request.json['subject']
    except:
        jsonify({"message": {"code": 0, "status": "缺少必要的参数"}})

    alipay = alipayInit.alipay_init()
    order_string = alipay.api_alipay_trade_wap_pay(
        subject=subject,
        out_trade_no=order_no,
        total_amount=price,
        return_url='https://test.com'
    )
    url = 'https://openapi.alipaydev.com/gateway.do?' + order_string

    return jsonify({"url": url, "message": {"code": 1, "status": "success"}})
